package com.blt.car.journey.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.journey.JourneyDetailActivity;
import com.blt.car.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.journey.bean.Travel;
import blt.app_core.api.journey.bean.TravelOrder;
import blt.app_core.api.message.bean.MessageInfo;
import blt.app_core.log.Logger;

/**
 * Created by xgp on 2017/6/19.
 */

public class JourHomeAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context context;
    private List<TravelOrder> travelOrderList;

    public JourHomeAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        travelOrderList = new ArrayList<>();
    }

    public void add(List<TravelOrder> list) {
//        travelOrderList.clear();
        travelOrderList.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        travelOrderList.clear();
    }

    @Override
    public int getCount() {
        return travelOrderList.size();
    }

    @Override
    public Object getItem(int position) {
        return travelOrderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        TravelHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_journey_home, parent, false);
            holder = new TravelHolder();
            holder.startSite = (TextView) view.findViewById(R.id.item_journey_from_site);
            holder.endSite = (TextView) view.findViewById(R.id.item_journey_finish_site);
            holder.startTime = (TextView) view.findViewById(R.id.item_journey_from_time);
            holder.endTime = (TextView) view.findViewById(R.id.item_journey_finish_time);
            holder.carNumber = (TextView) view.findViewById(R.id.item_journey_car_num);
            holder.account = (TextView) view.findViewById(R.id.item_journey_high_speed_fee);
            view.setTag(holder);
        } else {
            holder = (TravelHolder) view.getTag();
        }
        TravelOrder order = travelOrderList.get(position);
        String start = order.start.split("-")[1];
        holder.startSite.setText(start);
        String end = order.end.split("-")[1];
        holder.endSite.setText(end);
        holder.startTime.setText(StringUtils.getDay(order.startTime)
        );
        holder.endTime.setText(StringUtils.getDay(order.endTime));

        holder.carNumber.setText(context.getResources().getString(R.string.plate_number,
                order.carNo));
        holder.account.setText(context.getResources().getString(R.string.speed_cost,
                order.amount));
        view.setOnClickListener(new ItemClick(order.id));
        return view;
    }

    class ItemClick implements View.OnClickListener {
        private int travelId;

        public ItemClick(int id) {
            travelId = id;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, JourneyDetailActivity.class);
            intent.putExtra(JourneyDetailActivity.TRAVEL_ID, travelId);
            context.startActivity(intent);
        }
    }

    class TravelHolder {
        TextView startSite;
        TextView endSite;
        TextView startTime;
        TextView endTime;
        TextView carNumber;
        TextView account;
    }
}
